﻿using CodeCity.UserManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeCity.UserMangament.Repository
{
    public class RepositoryBase<T>
        where T : ModelBase
    {
        private readonly GameDbContext gameDbContext;

        protected DbSet<T> Query
        {
            get
            {
                return gameDbContext.Set<T>();
            }
        }

        public RepositoryBase(GameDbContext gameDbContext)
        {
            this.gameDbContext = gameDbContext;
        }


        public Task<int> SaveChangesAsync()
        {
            return gameDbContext.SaveChangesAsync();
        }

        public Task<List<T>> GetAllAsync()
        {
            return Query.ToListAsync();
        }

        public virtual Task<int> CountAsync()
        {
            return Query.CountAsync();
        }

        public Task<bool> ExistAsync(long id)
        {
            return Query.AnyAsync(x => x.UnitId == id);
        }

        public void Insert(List<T> items)
        {
            Query.AddRange(items);
        }

        public T Insert(T item)
        {
            return Query.Add(item).Entity;
        }
    }
}
